#include "DL_PD_Handler.h"
#include "string.h"
#include "DL_Message_Handler.h"
#include "IO_LINK_DataBuffer.h"
#include "AL_OD.h"
/****
typedef enum{
  PD_UpDated,
  PD_NO_UpDated
}PD_Flag_TYPE;
***********/
typedef struct{
  uint8_t   PDInAddress; //Interleave模式使用
  uint8_t  PDOutAddress;//Interleave模式使用
	 
}PD_PARAM_Typedef;



PD_PARAM_Typedef PD_PARAM[PORT_NUM];
PD_STATE_TYPE PD_STATE[PORT_NUM];

void DL_PD_Init(void){
    for(uint8_t i=0;i<PORT_NUM;i++){
        PD_STATE[i]=PD_Inactive_0;
        // PD_PARAM[i].PDOut_Flag=PD_NO_UpDated;
       // PD_PARAM[i].PDIn_Flag=PD_NO_UpDated; //标记所有的PD未更新，由服务Service_DL_PDoutPutUpDate等更新
    }
}

//传递PD数据到AL,
 void	 Event_DL_PDinputTransport_ind(uint8_t Port){
	 
 }
 
 //传递PD数据到AL
void Event_DL_PDCycle_ind(uint8_t Port){


}	



/********************转移函数******************/

void DL_PD_T1(uint8_t Port){
    if(MC_Struct[Port].PDin!=NULL){
        memset(MC_Struct[Port].PDin,0,MC_Struct[Port].PDInputLength);  //清空PDin
      return;
    }
    Event_PD(Port);
}


void DL_PD_T2(uint8_t Port){
	 //待更新
	//用于设置PD的single模式
	 
}


//PD输出
void DL_PD_T3(uint8_t Port){
   //准备PD.Req
    if(MC_Struct[Port].PDout!=NULL){
			//此处内存拷贝，相当于Take data from DL_PDOutputUpdate
        memcpy(MC_Struct[Port].PDout,&PD_BufferOut[Port][0],MC_Struct[Port].PDOutputLength);
        
    }
    

    Event_PD(Port);
}


//PD输入
void DL_PD_T3_1(uint8_t Port){

    //准备PD.Cnf
    if(MC_Struct[Port].PDin!=NULL){
       memcpy(&PD_BufferIn[Port][0],MC_Struct[Port].PDin,MC_Struct[Port].PDInputLength);
       Event_DL_PDinputTransport_ind(Port); //传递PD数据到AL,
       Event_DL_PDCycle_ind(Port);          //传递PD数据到AL
    } 

}

void DL_PD_T4(uint8_t Port){
    PD_PARAM[Port].PDInAddress=0;
    PD_PARAM[Port].PDOutAddress=0;
    if(MC_Struct[Port].PDInputLength==0){
        Event_Input_Data_Completed(Port);
    }
}

void DL_PD_T5(uint8_t Port){
 
    //准备PD.Req
    MC_Struct[Port].Dirction=Direction_Read;   //防止OD篡改

    Event_PD(Port);
}

void DL_PD_T5_1(uint8_t Port){
    //拷贝接收PD到缓冲区
    if(MC_Struct[Port].PDInputLength!=0){
        memcpy(&PD_BufferIn[Port][PD_PARAM[Port].PDInAddress],MC_Struct[Port].PDin,2);
        PD_PARAM[Port].PDInAddress+=2;
         
    }
    
     //判断PD cyc是否结束,并更新PDInAddress
     if(PD_PARAM[Port].PDInAddress>=MC_Struct[Port].PDInputLength){
        PD_PARAM[Port].PDInAddress=0; 
        Event_Input_Data_Completed(Port);

    }

}

void DL_PD_T6(uint8_t Port){
    //判断是否存在PDin，如果有，则想上层传递PDIn数据
    if(MC_Struct[Port].PDInputLength!=0){
        Event_DL_PDinputTransport_ind(Port);
        Event_DL_PDCycle_ind(Port);
    }
   //判断是否存在PDout，如果没有，立马回到PDin传输状态
    if(MC_Struct[Port].PDOutputLength==0){
        Event_Output_Data_Completed(Port);
    }
    
}

void DL_PD_T7(uint8_t Port){
     
    //准备PD.Req
    memcpy(MC_Struct[Port].PDout,PD_BufferOut[PD_PARAM[Port].PDOutAddress],2);
    MC_Struct[Port].Dirction=Direction_Write;   //防止OD篡改
   
    Event_PD(Port);
}

void DL_PD_T7_1(uint8_t Port){
        
    if(MC_Struct[Port].PDOutputLength!=0){
       PD_PARAM[Port].PDInAddress+=2;
         
    }
    
     //判断PD cyc是否结束,并更新PDInAddress
     if(PD_PARAM[Port].PDOutAddress>=MC_Struct[Port].PDOutputLength){
        PD_PARAM[Port].PDOutAddress=0; 
        Event_Output_Data_Completed(Port);

    }
}

void DL_PD_T8(uint8_t Port){
    Event_DL_PDCycle_ind(Port);
}


 
/*******输入Event*******************************/

void Event_PDTrig(uint8_t Port){
    if(PD_STATE[Port]==PD_Inactive_0){
        PD_STATE[Port]=subPD_Inactive_0;
        DL_PD_T1(Port);
        //PD_STATE[Port]=PD_Inactive_0;
        return;
    }
    if(PD_STATE[Port]==PDSingle_1){
        PD_STATE[Port]=subPDSingle_1;
        DL_PD_T3(Port);
        //PD_STATE[Port]=PDSingle_1;
        return;
    }
    if(PD_STATE[Port]==PDInInterleave_2){
        PD_STATE[Port]=subPDInInterleave_2;
        DL_PD_T5(Port);
        return;
    }
    if(PD_STATE[Port]==pDOutInterleave_3){
        PD_STATE[Port]=subPDOutInterleave_3;
        DL_PD_T7(Port);
        return;
    }
}


//自定义事件，当收到时从自定义子状态sub 返回原状态
void Event_PD_Return(uint8_t Port){
   if(PD_STATE[Port]==subPD_Inactive_0){
       PD_STATE[Port]=PD_Inactive_0;
       //DL_PD_T1_1(Port);
       return;
   }

   if(PD_STATE[Port]==subPDSingle_1){
       PD_STATE[Port]=PDSingle_1;
       DL_PD_T3_1(Port); //自定义子转移函数
       return;
   }

   if(PD_STATE[Port]==subPDInInterleave_2){
       PD_STATE[Port]=PDInInterleave_2;
       DL_PD_T5_1(Port); //自定义子转移函数
       return;
   }

   if(PD_STATE[Port]==subPDOutInterleave_3){
       PD_STATE[Port]=pDOutInterleave_3;
       DL_PD_T7_1(Port);//自定义子转移函数
   }

}

//自定义事件，当接收出错时候从自定义子状态sub 返回原状态
void Event_PD_Error_Return(uint8_t Port){
   if(PD_STATE[Port]==subPD_Inactive_0){
       PD_STATE[Port]=PD_Inactive_0;
       //DL_PD_T1_1(Port);
       return;
   }

   if(PD_STATE[Port]==subPDSingle_1){
       PD_STATE[Port]=PDSingle_1;
      // DL_PD_T3_1(Port); //自定义子转移函数
       return;
   }

   if(PD_STATE[Port]==subPDInInterleave_2){
       PD_STATE[Port]=PDInInterleave_2;
      // DL_PD_T5_1(Port); //自定义子转移函数
       return;
   }

   if(PD_STATE[Port]==subPDOutInterleave_3){
       PD_STATE[Port]=pDOutInterleave_3;
      // DL_PD_T7_1(Port);//自定义子转移函数
   }

}



void Event_PD_Conf_INACTIVE(uint8_t Port){
 
        PD_STATE[Port]=PD_Inactive_0;
    
    
}


void Event_PD_Conf_SINGLE(uint8_t Port){
    if(PD_STATE[Port]==PD_Inactive_0){
        
        PD_STATE[Port]=PDSingle_1;
			DL_PD_T2(Port);
        return;
    }
  
}

void Event_PD_Conf_INTERLEAVE(uint8_t Port){
    if(PD_STATE[Port]==PD_Inactive_0){
        
        PD_STATE[Port]=PDInInterleave_2;
        DL_PD_T4(Port);
    }
}

void Event_Input_Data_Completed(uint8_t Port){
    if(PD_STATE[Port]==PDInInterleave_2){
        
        PD_STATE[Port]=pDOutInterleave_3;
        DL_PD_T6(Port);
        return;
    }
  
}

void Event_Output_Data_Completed(uint8_t Port){
    if(PD_STATE[Port]==pDOutInterleave_3){
        
        PD_STATE[Port]=PDInInterleave_2;
        DL_PD_T8(Port);
        return;
    }
}

